<template>
  <div class="login-container">
    <!-- 左侧平台介绍 -->
    <div class="platform-intro">
      <div class="logo-container">
        <img src="@/assets/logo.png" alt="健康预测平台" class="logo" />
        <h1>健康预测平台</h1>
      </div>
      <div class="feature-cards">
        <div class="feature-card">
          <div class="feature-icon">✓</div>
          <h3>专业医疗支持</h3>
          <p>基于医疗数据的精准分析和预测</p>
        </div>
        <div class="feature-card">
          <div class="feature-icon">📊</div>
          <h3>数据可视化</h3>
          <p>直观展示健康数据和趋势变化</p>
        </div>
      </div>
    </div>

    <!-- 右侧登录表单 -->
    <div class="login-form">
      <div class="form-header">
        <img src="@/assets/logo.png" alt="健康预测平台" class="form-logo" />
        <h2>欢迎使用健康预测平台</h2>
        <p>请选择角色并登录</p>
      </div>

      <div class="form-group">
          <input 
            type="text" 
            id="username"
            name="username"
            v-model="username" 
            placeholder="请输入用户名"
            :class="{ 'error': loginError && !username }"
            class="form-control"
          />
        </div>
        
        <div class="form-group">
          <input 
            type="password" 
            id="password"
            name="password"
            v-model="password" 
            placeholder="请输入密码"
            :class="{ 'error': loginError && !password }"
            class="form-control"
          />
        </div>
        
        <!-- 角色选择 -->
        <div class="role-selector">
          <button 
            :class="['role-btn', { active: selectedRole === 'user' }]" 
            @click="selectedRole = 'user'"
          >
            普通用户
          </button>
          <button 
            :class="['role-btn', { active: selectedRole === 'statistician' }]" 
            @click="selectedRole = 'statistician'"
          >
            疾控统计员
          </button>
          <button 
            :class="['role-btn', { active: selectedRole === 'admin' }]" 
            @click="selectedRole = 'admin'"
          >
            管理员
          </button>
        </div>
        
        <div v-if="loginError" class="error-message">
          {{ loginError }}
        </div>
        
        <button @click="login" class="login-button">登录</button>
      
      <!-- 注册链接仅对普通用户显示 -->
      <div class="login-footer">
        <span v-if="selectedRole === 'user'">
          还没有账号？<router-link to="/register">立即注册</router-link>
        </span>
        <span v-else>
          如需账号，请联系系统管理员
        </span>
      </div>
      
      <!-- 认证信息标签 -->
      <div class="certification-tags">
        <span class="cert-tag">数据加密传输</span>
        <span class="cert-tag">合规认证</span>
        <span class="cert-tag">隐私保护</span>
      </div>
    </div>
  </div>

</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import api from '../utils/axiosConfig'

// 路由和状态管理
const router = useRouter()
const userStore = useUserStore()

// 响应式变量
const username = ref('')
const password = ref('')
const selectedRole = ref('user')
const loginError = ref('')

// 登录方法
const login = async () => {
  loginError.value = ''
  
  // 基本验证
  if (!username.value.trim()) {
    loginError.value = '请输入用户名'
    return
  }
  if (!password.value.trim()) {
    loginError.value = '请输入密码'
    return
  }
  
  try {
    // 按照后端API最新要求构造请求数据，使用user_name字段
    const loginData = {
      user_name: username.value.trim(),
      password: password.value.trim()
    }
    
    console.log('发送登录请求，使用用户名和密码', loginData)
    console.log('请求URL:', 'http://localhost:8080/api/user/login')
    
    // 使用api实例发送请求到正确的端点
    const response = await api.post('/user/login', loginData)
    
    console.log('登录请求成功，响应:', response)
    
    // 提取响应数据
    const data = response.data
    
    // 用户类型验证：根据user_type验证用户角色
    // 普通用户: user_type = 1, 疾控统计员: user_type = 2, 管理员: user_type = 3
    const userType = data.user_type || data.userType
    const roleMatch = {
      user: 1,
      statistician: 2,
      admin: 3
    }
    
    // 验证用户选择的角色是否与实际用户类型匹配
    if (userType !== roleMatch[selectedRole.value]) {
      loginError.value = '用户角色与选择的登录类型不匹配'
      return
    }
    
    // 保存用户信息到store
    userStore.login({
      username: data.username || data.user_name || username.value,
      role: data.role || selectedRole.value,
      userType: userType,
      token: data.token || '',
      userInfo: data
    })
    
    // 根据用户角色跳转到相应页面
    if (selectedRole.value === 'admin') {
      router.push('/admin/dashboard')
    } else if (selectedRole.value === 'statistician') {
      router.push('/statistician/dashboard')
    } else {
      router.push('/user/dashboard')
    }
  } catch (error) {
    console.error('登录失败:', error)
    loginError.value = '用户名或密码错误'
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  min-height: 100vh;
  background-color: #f0f8ff;
}

/* 左侧平台介绍 */
.platform-intro {
  flex: 1;
  background: linear-gradient(135deg, #0072c6 0%, #005a9e 100%);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px;
  color: #ffffff;
  position: relative;
  overflow: hidden;
}

/* 装饰元素 */
.platform-intro::before {
  content: '';
  position: absolute;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  top: -100px;
  right: 100px;
}

.platform-intro::after {
  content: '';
  position: absolute;
  width: 200px;
  height: 200px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  bottom: 50px;
  left: 50px;
}

.logo-container {
  text-align: center;
  margin-bottom: 60px;
  position: relative;
  z-index: 1;
}

.logo {
  width: 100px;
  height: 100px;
  margin-bottom: 20px;
  background-color: white;
  border-radius: 15px;
  padding: 10px;
}

.platform-intro h1 {
  font-size: 32px;
  font-weight: 700;
  margin: 0;
}

.feature-cards {
  display: flex;
  flex-direction: column;
  gap: 30px;
  width: 100%;
  max-width: 350px;
  position: relative;
  z-index: 1;
}

.feature-card {
  background-color: rgba(255, 255, 255, 0.1);
  padding: 25px;
  border-radius: 12px;
  text-align: center;
  -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
  transition: transform 0.3s ease;
}

.feature-card:hover {
  transform: translateY(-5px);
}

.feature-icon {
  width: 60px;
  height: 60px;
  margin: 0 auto 15px;
  background-color: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
}

.feature-card h3 {
  font-size: 20px;
  margin-bottom: 10px;
  margin-top: 0;
}

.feature-card p {
  font-size: 15px;
  opacity: 0.9;
  margin: 0;
}

/* 右侧登录表单 */
.login-form {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px;
  background-color: #ffffff;
  box-shadow: -5px 0 25px rgba(0, 0, 0, 0.05);
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
  width: 100%;
  max-width: 350px;
}

.form-logo {
  width: 60px;
  height: 60px;
  margin-bottom: 15px;
}

.form-header h2 {
  font-size: 24px;
  color: #0072c6;
  margin-bottom: 10px;
  margin-top: 0;
}

.form-header p {
  font-size: 16px;
  color: #666666;
  margin: 0;
}

.form-group {
  width: 100%;
  max-width: 350px;
  margin-bottom: 20px;
}

.form-control {
  width: 100%;
  padding: 14px 16px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.form-control:focus {
  outline: none;
  border-color: #0072c6;
  box-shadow: 0 0 0 3px rgba(0, 114, 198, 0.1);
}

.form-control.error {
  border-color: #e74c3c;
}

.role-selector {
  display: flex;
  gap: 10px;
  margin-bottom: 25px;
  max-width: 350px;
  width: 100%;
}

.role-btn {
  flex: 1;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background-color: #f5f5f5;
  cursor: pointer;
  font-size: 15px;
  transition: all 0.3s ease;
}

.role-btn.active {
  background-color: #0072c6;
  color: #ffffff;
  border-color: #0072c6;
  box-shadow: 0 4px 12px rgba(0, 114, 198, 0.2);
}

.error-message {
  color: #e74c3c;
  font-size: 14px;
  margin-bottom: 15px;
  max-width: 350px;
  width: 100%;
  padding: 8px 12px;
  background-color: #fff5f5;
  border-radius: 6px;
}

.login-button {
  width: 100%;
  max-width: 350px;
  padding: 14px;
  background: linear-gradient(135deg, #0072c6 0%, #005a9e 100%);
  color: #ffffff;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 10px;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 114, 198, 0.3);
}

.login-footer {
  margin-top: 25px;
  text-align: center;
  color: #666666;
  font-size: 14px;
  width: 100%;
  max-width: 350px;
}

.login-footer a {
  color: #0072c6;
  text-decoration: none;
  font-weight: 600;
}

.login-footer a:hover {
  text-decoration: underline;
}

.certification-tags {
  display: flex;
  gap: 12px;
  margin-top: 35px;
  flex-wrap: wrap;
  justify-content: center;
  width: 100%;
  max-width: 350px;
}

.cert-tag {
  font-size: 13px;
  color: #666666;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px 12px;
  background-color: #f5f9ff;
  border-radius: 20px;
  border: 1px solid #e6edf7;
}

.cert-tag::before {
  content: '🔒';
  font-size: 12px;
}

/* 响应式适配 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }
  
  .platform-intro {
    padding: 30px 20px;
  }
  
  .feature-cards {
    max-width: 100%;
  }
  
  .login-form {
    padding: 30px 20px;
  }
}
</style>